package org.gis.mapserver.rendercontext.png;

import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;


import org.gis.mapserver.base.ContextType;
import org.gis.mapserver.responsewriter.ResponseWriter;
import org.gis.mapserver.layer.element.Path;
import org.gis.mapserver.layer.element.Position;
import org.gis.mapserver.rendercontext.RenderingContext;
import org.gis.mapserver.responsewriter.ResponseWriterFactory;

/**
 *
 * @author gus
 */
class PngRenderingContext implements RenderingContext
{
    private BufferedImage bufferedImage;
    private Graphics2D painter;
    
    private static int X_DIMENSION  = 800;
    private static int Y_DIMENSION  = 600;

    public PngRenderingContext()
    {
        bufferedImage = new BufferedImage(X_DIMENSION, Y_DIMENSION, BufferedImage.TYPE_INT_ARGB);
        painter = bufferedImage.createGraphics();
    }

    public void drawPath( Path path )
    {
        painter.drawPolygon(new Polygon(path.getXPoints(), path.getYPoints(), X_DIMENSION));
    }

    public void drawTitle( Position pos, String text )
    {
//        painter.drawString(text, pos, X_DIMENSION);
    }

    public void drawImage( BufferedImage image, int x, int y )
    {
        double scaleX = (double) bufferedImage.getWidth() / (double) image.getWidth();
        double scaleY = (double) bufferedImage.getHeight() / (double) image.getHeight();

        AffineTransform transform = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
        painter.drawImage(image, op, x, y);
    }

    public RenderingContext getParentRenderingContext()
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ContextType getType()
    {
        return ContextType.PNG;
    }

    public ResponseWriter getResponseWriter()
    {
        return ResponseWriterFactory.makeBinaryResponseWriter(bufferedImage);
    }

    public void dispose()
    {
    }
}
